package com.awccis.admin.controller;

import com.awccis.admin.model.AdminBean;
import com.awccis.admin.model.DriverLocationBean;
import com.awccis.admin.model.commission.CommissionBean;
import com.awccis.admin.model.commission.DriverCommissionBean;
import com.awccis.admin.model.pay.PaySimpleInfoBean;
import com.awccis.admin.model.price.PriceBean;
import com.awccis.admin.service.AdminService;
import com.awccis.common.controller.BaseUploadController;
import com.awccis.common.pool.DriverPool;
import com.awccis.common.session.DriverSession;
import com.awccis.common.utils.Constants;
import com.awccis.common.utils.MD5Utils;
import com.awccis.driver.dao.DriverDao;
import com.awccis.driver.model.DriverDetailInfo;
import com.awccis.driver.model.DriverInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.*;

/**
 * @Author: 纯牛奶丶
 * @Date: 2018-04-14 16:58
 * @Since: 1.0
 * @Description:
 **/
@Controller
@RequestMapping(value = "/admin")
public class AdminController extends BaseUploadController {

    /**
     * 日志打印对象
     */
    private Logger logger = LoggerFactory.getLogger(AdminController.class);

    @Resource
    AdminService adminService;

    @Resource
    DriverDao driverDao;


    /**
     * 管理员登录
     *
     * @param adminBean {"adminName":"adminOne", "adminPasswd":"admi"}
     * @return
     */
    @RequestMapping(value = "/adminLogin", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Object adminLogin(@RequestBody AdminBean adminBean) {
        logger.info("管理员登录信息：" + adminBean.toString());

        Map<String, Object> map = new HashMap<>();

        adminBean.setAdminPasswd(MD5Utils.getMD5(adminBean.getAdminPasswd()));
        AdminBean admin = adminService.adminLogin(adminBean);
        if (admin == null) {
            map.put(Constants.MAP_KEY_RESULT, false);
            map.put(Constants.MAP_KEY_MESSAGE, "非法请求值");
        } else {
            map.put(Constants.MAP_KEY_RESULT, true);
            map.put(Constants.MAP_KEY_MESSAGE, admin);
        }
        return map;
    }

    /**
     * 管理员重置密码
     *
     * @param map {"":""}
     * @return
     */
    @RequestMapping(value = "/changePwd", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Object changePwd(@RequestBody HashMap map) {
        logger.info("管理员重置密码！");
        HashMap result = new HashMap(2);
        String id = "id";
        String oldPassWord = "oldPassWord";
        String newPassWord = "newPassWord";

        if (map.get(id) != null && !"".equals(map.get(id))
                && map.get(oldPassWord) != null && !"".equals(map.get(oldPassWord))
                && map.get(newPassWord) != null && !"".equals(map.get(newPassWord))) {
            map.replace(oldPassWord, MD5Utils.getMD5(map.get(oldPassWord).toString()));
            map.replace(newPassWord, MD5Utils.getMD5(map.get(newPassWord).toString()));
            if (adminService.changePwd(map)) {
                result.put(Constants.MAP_KEY_RESULT, true);
                result.put(Constants.MAP_KEY_MESSAGE, "密码修改成功!");
                return result;
            } else {
                result.put(Constants.MAP_KEY_RESULT, false);
                result.put(Constants.MAP_KEY_MESSAGE, "密码修改失败!");
                return result;
            }
        } else {
            result.put(Constants.MAP_KEY_RESULT, false);
            result.put(Constants.MAP_KEY_MESSAGE, Constants.MAP_VALUE_ILLEGAL_REQUEST);
            return result;
        }
    }

    /**
     * 获取所有的管理员
     *
     * @param hashMap {"level": 1}
     * @return list
     */
    @RequestMapping(value = "/getAdmins", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Object getAdmins(@RequestBody HashMap hashMap) {
        HashMap result = new HashMap();
        List<AdminBean> list;
        String level = "level";
        if (hashMap.get(level) != null) {
            list = adminService.getAllAdminInfo((Integer) hashMap.get(level));
            result.put(Constants.MAP_KEY_RESULT, true);
            result.put(Constants.MAP_KEY_MESSAGE, list);
        } else {
            result.put(Constants.MAP_KEY_RESULT, false);
            result.put(Constants.MAP_KEY_MESSAGE, Constants.MAP_VALUE_ILLEGAL_REQUEST);
        }
        return result;
    }

    /**
     * 通过id删除管理员
     *
     * @param hashMap {"id": 4}
     * @return map
     */
    @RequestMapping(value = "/deleteAdmin", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Object deleteAdmin(@RequestBody HashMap<String, Integer> hashMap) {
        logger.info("删除admin：" + hashMap);
        Map<String, Object> map = new HashMap<>(2);
        boolean result;
        if (hashMap.get("id") != null) {
            result = adminService.deleteAdminById(Integer.parseInt(hashMap.get("id").toString()));
            map.put(Constants.MAP_KEY_RESULT, result);
        }
        return map;
    }

    /**
     * 添加管理员
     *
     * @param hashMap {"adminName":"txxx","adminPasswd":"123456878","adminPowerLevel":3}
     * @return true false
     */
    @RequestMapping(value = "/addAdmin", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Object addAdmin(@RequestBody HashMap hashMap) {
        logger.info("添加管理员:" + hashMap);

        HashMap<String, Object> map = new HashMap(2);
        String adminName = "adminName";
        String adminPasswd = "adminPasswd";
        String adminPowerLevel = "adminPowerLevel";
        boolean result;
        if (hashMap.get(adminName) != null && !"".equals(hashMap.get(adminName)) &&
                hashMap.get(adminPasswd) != null && !"".equals(hashMap.get(adminPasswd)) &&
                hashMap.get(adminPowerLevel) != null && !"".equals(hashMap.get(adminPowerLevel))) {
            hashMap.replace(adminPasswd, MD5Utils.getMD5(hashMap.get(adminPasswd).toString()));
            result = adminService.addAdmin(hashMap);
            map.put(Constants.MAP_KEY_RESULT, result);
        }
        return map;
    }


    /**
     * 查询所有乘客用户
     *
     * @param hashMap {"AWCCIS_CODE": ********}
     * @return {}
     */
    @RequestMapping(value = "/getAllCustomers", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Object getAllCustomers(@RequestBody HashMap hashMap) {
        logger.info("查询乘客用户:" + hashMap);
        Map<String, Object> map = new HashMap<>(2);
        String awccisCode = "AWCCIS_CODE";
        if (hashMap.get(awccisCode) == null) {
            map.put(Constants.MAP_KEY_RESULT, false);
            map.put(Constants.MAP_KEY_MESSAGE, "请求参数错误!");
        } else {
            if (Constants.AWCCIS_CODE.equals(hashMap.get(awccisCode).toString())) {
                map.put(Constants.MAP_KEY_RESULT, true);
                map.put(Constants.MAP_KEY_MESSAGE, adminService.getAllCustomers());
                System.out.println(map);
            } else {
                map.put(Constants.MAP_KEY_RESULT, false);
                map.put(Constants.MAP_KEY_MESSAGE, "系统识别码错误!");
            }
        }
        return map;
    }

    /**
     * 获取所有的司机列表
     *
     * @param hashMap {"AWCCIS_CODE": "1a5da935ac641f2c46d5096f08d0c40b15234072"}
     * @return {}
     */
    @RequestMapping(value = "/getAllDrivers", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Object getAllDrivers(@RequestBody HashMap hashMap) {
        logger.info("查询所有司机：" + hashMap);
        Map<String, Object> map = new HashMap<>(2);
        String awccisCode = "AWCCIS_CODE";
        if (hashMap.get(awccisCode) == null) {
            map.put(Constants.MAP_KEY_RESULT, false);
            map.put(Constants.MAP_KEY_MESSAGE, "请求参数错误!");
        } else {
            if (Constants.AWCCIS_CODE.equals(hashMap.get(awccisCode).toString())) {
                map.put(Constants.MAP_KEY_RESULT, true);
                map.put(Constants.MAP_KEY_MESSAGE, adminService.getAllDrivers());
            } else {
                map.put(Constants.MAP_KEY_RESULT, false);
                map.put(Constants.MAP_KEY_MESSAGE, "系统识别码错误!");
            }
        }
        return map;
    }

    /**
     * 模糊查询司机简单信息
     *
     * @param hashMap {"type": "nickName", "value": "222"}
     * @return {}
     */
    @RequestMapping(value = "/fuzzySearchDriver", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Map<String, Object> fuzzySearchDriver(@RequestBody HashMap<String, String> hashMap) {
        logger.info("模糊查询司机：" + hashMap);
        System.out.println(hashMap);

        Map<String, Object> map = new HashMap<>();

        String type = hashMap.get("type");
        String value = hashMap.get("value");
        switch (type) {
            case "nickName":
                LinkedList<DriverInfo> linkedList;
                try {
                    linkedList = adminService.selectDriverFuzzy(type, Integer.parseInt(value));
                } catch (NumberFormatException e) {
                    logger.warn("根据nickName查询司机信息类型转换异常\n" + e.toString());
                    System.out.println("根据nickName查询司机信息类型转换异常");
                    map.put(Constants.MAP_KEY_RESULT, false);
                    map.put(Constants.MAP_KEY_MESSAGE, "根据" + value + "条件查询nickName转换失败！");
                    return map;
                }
                if (linkedList.size() > 0) {
                    map.put(Constants.MAP_KEY_RESULT, true);
                    map.put(Constants.MAP_KEY_MESSAGE, linkedList);
                } else {
                    map.put(Constants.MAP_KEY_RESULT, false);
                    map.put(Constants.MAP_KEY_MESSAGE, "根据" + value + "条件查询nickName不存在！");
                }
                break;
            default:
                break;
        }

        return map;
    }

    /**
     * 获取司机的详细信息
     *
     * @param hashMap {"driverId": 31}
     * @return {"":}
     */
    @RequestMapping(value = "/getDriverDetailInfo", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Object getDriverDetailInfo(@RequestBody HashMap hashMap) {
        logger.info("获取司机的详细信息：" + hashMap);
        Map<String, Object> map = new HashMap<>(2);
        String driverId = "driverId";
        if (hashMap.get(driverId) != null) {
            int id = Integer.parseInt(hashMap.get(driverId).toString());
            DriverDetailInfo driverDetailInfo = null;
            DriverSession driverSession = null;

            try {
                driverDetailInfo = adminService.getDriverDetailInfo(id);
            } catch (NumberFormatException e) {
                logger.warn("获取司机详细信息异常：" + e);
                map.put(Constants.MAP_KEY_RESULT, false);
                map.put(Constants.MAP_KEY_MESSAGE, "请求参数异常！");
            }

            if (DriverPool.isContainById(id)) {
                driverSession = DriverPool.getSessionById(id);

                //driverDetailInfo.setDriverIntegralCount(driverSession.getDriverIntegral_count());
                driverDetailInfo.setLocationLaitude(driverSession.getLat());
                driverDetailInfo.setLocationLongitude(driverSession.getLng());
                driverDetailInfo.setDriverIsOnline(0);
                driverDetailInfo.setDriverIsWork(driverSession.isWork() ? 0 : 1);
                driverDetailInfo.setStarLevel(driverSession.getStarLevel());
            } else {
                Integer level = driverDao.selectStarLevel(id);
                driverDetailInfo.setStarLevel(level == 0 ? 5 : level);
                driverDetailInfo.setDriverIsOnline(1);
                driverDetailInfo.setDriverIsWork(1);
                driverDetailInfo.setLocationName("未知");
            }

            map.put(Constants.MAP_KEY_RESULT, true);
            map.put(Constants.MAP_KEY_MESSAGE, driverDetailInfo);
        } else {
            map.put(Constants.MAP_KEY_RESULT, false);
            map.put(Constants.MAP_KEY_MESSAGE, Constants.MAP_VALUE_ILLEGAL_REQUEST);
        }
        return map;
    }

    /**
     * 查看资源图片
     *
     * @param request  请求
     * @param response 响应
     * @param image    image 图片路径
     */
    @RequestMapping(value = "/showPic")
    public void showPic(HttpServletRequest request, HttpServletResponse response, @RequestParam String image) {
        logger.info("路径：" + image);
        //String fileUrl = request.getServletContext().getRealPath("")+image;
        String fileUrl = "/var/tomcat/awccis/upload/" + image;
        String[] strArr = image.split("\\.");
        try {
            File filePath = new File(fileUrl);
            if (filePath.exists()) {
                //读图片
                FileInputStream inputStream = new FileInputStream(filePath);
                int available = inputStream.available();
                byte[] data = new byte[available];
                inputStream.read(data);
                inputStream.close();
                //写图片
                response.setContentType("image/" + strArr[1]);
                OutputStream stream = new BufferedOutputStream(response.getOutputStream());
                stream.write(data);
                stream.flush();
                stream.close();
            }
        } catch (Exception e) {
            logger.warn("获取图片异常：" + e);
        }
    }

    /**
     * 根据司机的driverId注销司机的账户
     *
     * @param hashMap {"driverId": 11}
     * @return {"result": true}
     */
    @RequestMapping(value = "/logoffDriverAccount", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Object logoffDriverAccount(@RequestBody HashMap<String, Integer> hashMap) {
        logger.info("注销司机账户的driverId:" + hashMap);
        Map<String, Object> map = new HashMap<>(2);
        String driverId = "driverId";
        if (hashMap.get(driverId) != null) {
            try {
                //在注销账户时候同时也要删除内存池中的driver
                int id = hashMap.get(driverId);
                boolean b = adminService.logoffDriverAccount(hashMap.get(driverId)) == 1;
                if (b) {
                    if (DriverPool.isContainById(id)) {
                        DriverPool.deleteDriverById(id);
                    }
                    map.put(Constants.MAP_KEY_RESULT, true);
                    map.put(Constants.MAP_KEY_MESSAGE, "注销成功！");
                } else {
                    map.put(Constants.MAP_KEY_RESULT, false);
                    map.put(Constants.MAP_KEY_MESSAGE, "注销失败！");
                }
            } catch (NullPointerException e) {
                logger.warn("注销司机异常：" + e);
                map.put(Constants.MAP_KEY_RESULT, false);
                map.put(Constants.MAP_KEY_MESSAGE, "手机号不存在！");
            }
        }
        return map;
    }

    /**
     * 获取汇率
     *
     * @param hashMap {"AWCCIS_CODE":"1a5da935ac641f2c46d5096f08d0c40b15234072"}
     * @return {
     * "result": true,
     * "message": 4.9678，
     * }
     */
    @RequestMapping(value = "/exchangeRate", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Map exchangeRate(HttpServletRequest request, @RequestBody HashMap<String, String> hashMap) {
        logger.info("获取汇率：" + hashMap);
        Map map = new HashMap(2);

        String awccisCode = "AWCCIS_CODE";
        double exchangeRate = 0;
        if (hashMap.get(awccisCode) != null && Constants.AWCCIS_CODE.equals(hashMap.get(awccisCode))) {
            ServletContext servletContext = request.getServletContext();
            if (servletContext.getAttribute("exchangeRate") == null) {
                exchangeRate = adminService.getExchangeRate();
                servletContext.setAttribute("exchangeRate", exchangeRate);
            }
            exchangeRate = (double) servletContext.getAttribute("exchangeRate");
            map.put(Constants.MAP_KEY_RESULT, true);
            map.put(Constants.MAP_KEY_MESSAGE, exchangeRate);
        } else {
            map.put(Constants.MAP_KEY_RESULT, false);
            map.put(Constants.MAP_KEY_MESSAGE, "非法请求值");
        }
        return map;
    }

    /**
     * 获取当前所有的在线的司机的位置和简单信息
     *
     * @param hashMap {"AWCCIS_CODE":"1a5da935ac641f2c46d5096f08d0c40b15234072"}
     * @return
     */
    @RequestMapping(value = "/showDriverLocation", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Map showDriverLocation(@RequestBody HashMap<String, String> hashMap) {
        logger.info("获取当前所有在线的司机简单司机： code：" + hashMap);
        Map map = new HashMap(2);

        String awccisCode = "AWCCIS_CODE";
        List<DriverSession> list;

        if (hashMap.get(awccisCode) != null && Constants.AWCCIS_CODE.equals(hashMap.get(awccisCode))) {
            list = DriverPool.getSimpleAllOnlineDriver();
            if (list.size() > 0) {
                map.put(Constants.MAP_KEY_RESULT, true);
                map.put(Constants.MAP_KEY_MESSAGE, list);
            } else {
                map.put(Constants.MAP_KEY_RESULT, false);
                map.put(Constants.MAP_KEY_MESSAGE, "当前还没有在线的司机！");
            }
        } else {
            map.put(Constants.MAP_KEY_RESULT, false);
            map.put(Constants.MAP_KEY_MESSAGE, "请求值非法！");
        }
        return map;
    }

    /**
     * 获取价格体系
     *
     * @param hashMap {"AWCCIS_CODE":"1a5da935ac641f2c46d5096f08d0c40b15234072"}
     * @return map
     */
    @RequestMapping(value = "/getPriceRule", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Map getPriceRule(@RequestBody HashMap<String, String> hashMap) {
        logger.info("获取价格体系：" + hashMap);

        Map map = new HashMap(2);
        String awccisCode = "AWCCIS_CODE";
        List<PriceBean> list;
        if (hashMap.get(awccisCode) != null && Constants.AWCCIS_CODE.equals(hashMap.get(awccisCode))) {
            list = adminService.getAllPriceRule();
            if (list.size() == 0) {
                map.put(Constants.MAP_KEY_RESULT, false);
                map.put(Constants.MAP_KEY_MESSAGE, "当前价格体系有误！");
            } else {
                map.put(Constants.MAP_KEY_RESULT, true);
                map.put(Constants.MAP_KEY_MESSAGE, list);
            }
        }

        return map;
    }

    /**
     * 更改价格体系 （最初方案）
     *
     * @param priceBean {}
     * @return {}
     */
    @RequestMapping(value = "/updatePrice", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Map updatePrice(@RequestBody PriceBean priceBean) {
        logger.info("更改价格体系：" + priceBean.toString());

        Map map = new HashMap();

        map.put("result", adminService.updatePrice(priceBean));

        return map;
    }

    /**
     * 更该价格体系 （最终方案）
     *
     * @param hashMap {"list": [ {"priceId": 1, "}, {}]}
     * @return {}
     */
    @RequestMapping(value = "/updatePriceRule", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Map updatePriceRule(@RequestBody HashMap<String, List<PriceBean>> hashMap) {
        logger.info("更改价格体系，（新表）：" + hashMap);

        Map map = new HashMap(2);

        List<PriceBean> list = null;
        if (hashMap.get("list") != null) {
            list = hashMap.get("list");
        } else {
            map.put(Constants.MAP_KEY_RESULT, false);
            map.put(Constants.MAP_KEY_MESSAGE, "非法请求值");
            return map;
        }
        Boolean result = adminService.updatePriceRule(list);
        map.put(Constants.MAP_KEY_RESULT, result);
        return map;

    }

    /**
     * 获取抽成体系
     *
     * @param hashMap {"AWCCIS_CODE":"1a5da935ac641f2c46d5096f08d0c40b15234072"}
     * @return map
     */
    @RequestMapping(value = "/getCommissionRule", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Map getCommissionRule(@RequestBody HashMap<String, String> hashMap) {
        logger.info("获取抽成体系： " + hashMap);

        Map map = new HashMap(2);

        String awccisCode = "AWCCIS_CODE";
        List<CommissionBean> list;
        if (hashMap.get(awccisCode) != null && Constants.AWCCIS_CODE.equals(hashMap.get(awccisCode))) {
            list = adminService.getAllCommissionRule();
            if (list.size() == 0) {
                map.put(Constants.MAP_KEY_RESULT, false);
                map.put(Constants.MAP_KEY_MESSAGE, "当前抽成体系有误！");
            } else {
                map.put(Constants.MAP_KEY_RESULT, true);
                map.put(Constants.MAP_KEY_MESSAGE, list);
            }
        }
        return map;
    }

    /**
     * 更改抽成体系
     *
     * @param hashMap {
     *                "commission":
     *                [
     *                {
     *                "commissionId": 1,
     *                "commissionRangeStart": 0,
     *                "commissionRangeEnd":60,
     *                "commissionRatio": 0.17
     *                },
     *                {
     *                "commissionId": 2,
     *                "commissionRangeStart": 60,
     *                "commissionRangeEnd":210,
     *                "commissionRatio": 0.16
     *                },
     *                {
     *                "commissionId": 3,
     *                "commissionRangeStart": 210,
     *                "commissionRangeEnd":400,
     *                "commissionRatio": 0.15
     *                },
     *                {
     *                "commissionId": 4,
     *                "commissionRangeStart": 400,
     *                "commissionRangeEnd":700,
     *                "commissionRatio": 0.14
     *                },
     *                {
     *                "commissionId": 5,
     *                "commissionRangeStart": 700,
     *                "commissionRangeEnd":1100,
     *                "commissionRatio": 0.13
     *                },
     *                {
     *                "commissionId": 6,
     *                "commissionRangeStart": 1100,
     *                "commissionRangeEnd":999999,
     *                "commissionRatio": 0.12
     *                }
     *                ]
     *                <p>
     *                }
     * @return map
     */
    @RequestMapping(value = "/setCommissionRule", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Map setCommissionRule(@RequestBody HashMap<String, List<CommissionBean>> hashMap) {
        logger.info("更改抽成体系：" + hashMap);

        Map map = new HashMap(2);

        String commission = "commission";
        if (hashMap.get(commission) != null) {
            List<CommissionBean> list = hashMap.get(commission);
            if (list.size() >= 1) {
                boolean result = adminService.updateCommissionRule(list);
                map.put(Constants.MAP_KEY_RESULT, true);
            }
        } else {
            map.put(Constants.MAP_KEY_RESULT, false);
            map.put(Constants.MAP_KEY_MESSAGE, "非法请求值！");
        }

        return map;
    }

    /**
     * 获取所有需要向平台支付费用的司机
     *
     * @param hashMap {"AWCCIS_CODE":"1a5da935ac641f2c46d5096f08d0c40b15234072", "selectTimes":"second"}
     * @return
     */
    @RequestMapping(value = "/getAllDriverNeedPay", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Map getAllDriverNeedPay(@RequestBody HashMap<String, String> hashMap) {
        logger.info("获取所有需要向平台支付费用的司机：" + hashMap);

        Map map = new HashMap(2);

        String awccisCode = "AWCCIS_CODE";
        String selectTimes = "selectTimes";
        List<PaySimpleInfoBean> list = null;
        if (hashMap.get(awccisCode) != null
                && Constants.AWCCIS_CODE.equals(hashMap.get(awccisCode))
                && hashMap.get(selectTimes) != null) {
            /*
                获取当前年份与周数
             */
            Calendar cl = Calendar.getInstance();
            int week = cl.get(Calendar.WEEK_OF_YEAR) - 1;
            cl.add(Calendar.DAY_OF_MONTH, -7);
            int year = cl.get(Calendar.YEAR);
            if (week < cl.get(Calendar.WEEK_OF_YEAR)) {
                year += 1;
            }
            String payWeek = year + "年第" + week + "周";

            switch (hashMap.get(selectTimes)) {
                case "0001":
                    list = adminService.getAllDriverNeedPay(payWeek);
                    //list = adminService.getAllDriverNeedPayDirectly(payWeek);
                    break;
                case "second":
                    list = adminService.getAllDriverNeedPayDirectly(payWeek);
                    break;
                default:
                    list = new ArrayList<>();
                    break;
            }
            if (list.size() == 0) {
                map.put(Constants.MAP_KEY_RESULT, false);
                map.put(Constants.MAP_KEY_MESSAGE, "当前未有需要支付平台费用的司机！");
            } else {
                map.put(Constants.MAP_KEY_RESULT, true);
                map.put(Constants.MAP_KEY_MESSAGE, list);
            }
        }
        return map;
    }

    /**
     * 获取司机的commission
     *
     * @param hashMap {"AWCCIS_CODE":"1a5da935ac641f2c46d5096f08d0c40b15234072"}
     * @return {}
     */
    @RequestMapping(value = "/getDriverCommission", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Map<String, Object> getDriverCommission(@RequestBody HashMap<String, String> hashMap) {
        logger.info("获取driver的commission:" + hashMap);

        Map<String, Object> map = new HashMap<>();

        String awccisCode = "AWCCIS_CODE";
        if (hashMap.get(awccisCode) != null && Constants.AWCCIS_CODE.equals(hashMap.get(awccisCode))) {
            List<DriverCommissionBean> list = adminService.getAllDriverCommission();
            if (list.size() > 0) {
                map.put(Constants.MAP_KEY_RESULT, true);
                map.put(Constants.MAP_KEY_MESSAGE, list);
            } else {
                map.put(Constants.MAP_KEY_RESULT, false);
                map.put(Constants.MAP_KEY_MESSAGE, "当前driver的抽成比例为空！");
            }
        } else {
            map.put(Constants.MAP_KEY_RESULT, false);
            map.put(Constants.MAP_KEY_MESSAGE, "请求参数非法！");
        }

        return map;
    }

    @RequestMapping(value = "/testCommission", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Map<String, Object> testCommission() {
        logger.info("测试司机的Commission");

        Map<String, Object> map = new HashMap<>();
        /*
                获取当前年份与周数
             */
        Calendar cl = Calendar.getInstance();
        int week = cl.get(Calendar.WEEK_OF_YEAR) - 1;
        cl.add(Calendar.DAY_OF_MONTH, -7);
        int year = cl.get(Calendar.YEAR);
        if (week < cl.get(Calendar.WEEK_OF_YEAR)) {
            year += 1;
        }
        String payWeek = year + "年第" + week + "周";
        map.put(Constants.MAP_KEY_RESULT, adminService.initDriverCommission(payWeek));
        return map;
    }

    /**
     * 更改司机的commission
     *
     * @param commissionBean {}
     * @return
     */
    @RequestMapping(value = "/changeDriverCommission", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Map<String, Object> changeDriverCommission(@RequestBody DriverCommissionBean commissionBean) {
        logger.info("更改driver的Commission" + commissionBean.toString());
        System.out.println(commissionBean.toString());
        Map<String, Object> map = new HashMap<>();

        //汇率最高值（应有数据表中获取）
        double maxCommission = 0.17;
        double num = 0.005 * 1000;

        if (commissionBean.getDriverId() <= 0) {
            map.put(Constants.MAP_KEY_RESULT, false);
            map.put(Constants.MAP_KEY_MESSAGE, "请求参数driver为空！");
            return map;
        }
        if (commissionBean.getPunishCommission() <= 0 || commissionBean.getPunishCommission() > maxCommission) {
            map.put(Constants.MAP_KEY_RESULT, false);
            map.put(Constants.MAP_KEY_MESSAGE, "请求参数初始汇率值非法！");
            return map;
        }
        if (commissionBean.getPunishCommission() <= 0 || commissionBean.getPunishCommission() > maxCommission) {
            map.put(Constants.MAP_KEY_RESULT, false);
            map.put(Constants.MAP_KEY_MESSAGE, "请求参数初始汇率值非法！");
            return map;
        }
        if ((commissionBean.getPunishMark() * 1000) % num != 0) {
            map.put(Constants.MAP_KEY_RESULT, false);
            map.put(Constants.MAP_KEY_MESSAGE, "请求参数更改汇率值非法！");
            return map;
        }
        if (commissionBean.getPunishSum() <= 0 || commissionBean.getPunishSum() > maxCommission) {
            map.put(Constants.MAP_KEY_RESULT, false);
            map.put(Constants.MAP_KEY_MESSAGE, "请求参数最终汇率值非法！");
            return map;
        }

        boolean result = adminService.setDriverCommission(commissionBean);

        map.put(Constants.MAP_KEY_RESULT, result);

        return map;
    }

    /**
     * 记录访问量
     *
     * @param requestMap 请求参数
     * @param request    request
     * @return 访问信息
     */
    @RequestMapping(value = "/visitInfo", produces = Constants.REQUEST_PRODUCES)
    public @ResponseBody
    Object visitInfo(@RequestBody Map requestMap, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("date", System.currentTimeMillis());
        map.put("os", requestMap.get("os"));
        map.put("model", requestMap.get("model"));
        map.put("ip", request.getRemoteAddr());
        adminService.recordVisitInfo(map);

        return map;
    }

}